package com.jlkf.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jlkf.dao.AdvertisingManagerMapper;
import com.jlkf.dao.ConfigManagerMapper;
import com.jlkf.pojo.AdvertisingManager;
import com.jlkf.pojo.ConfigManager;
import com.jlkf.service.AdvertisingManagerService;
import com.jlkf.service.util.StringTools;
import com.platform.server.response.response.pojo.ReturnData;

import net.sf.json.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Service
public class AdvertisingManagerServiceImpl implements AdvertisingManagerService{

	@Autowired
	private AdvertisingManagerMapper advertisingManagerMapper;
	@Autowired
	private ConfigManagerMapper configManagerMapper;
	@Autowired
	private JedisPool jedisPool; 
	@Override
	public ReturnData getAdvertisingManager(Integer type) {
		Map<String,Object> map= new HashMap<>();
		map.put("type", type);
		map.put("status", 1);
		try {
			List<AdvertisingManager> list=advertisingManagerMapper.getAdvertisingManager(map);
			return new ReturnData(list,200,"广告页");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}
	    //接单倒计时
		@Override
		public ReturnData singleCountdown() {
			Map<String,Object> map= new HashMap<>();
			Integer time = 0;
			Jedis jedis = null;
			if(jedis == null){
				jedis=	jedisPool.getResource();
			}
			try {
				/* List<ConfigManager> cm=configManagerMapper.getList();
				 if(cm.size()>0){
					 time=cm.get(0).getOrderTime();
				 }*/
				
				String configManager=jedis.get("configManager");
				ConfigManager cm=null;
				if(StringTools.isEmpty(configManager)){
					 cm = configManagerMapper.selectByPrimaryKey(1);
						JSONObject jo = JSONObject.fromObject(cm);
						jedis.set("configManager", jo.toString());
						time = cm.getOrderTime();
				}else{
					JSONObject jo = JSONObject.fromObject(configManager);
					cm = (ConfigManager) JSONObject.toBean(jo, ConfigManager.class);
					time = cm.getOrderTime();
				}
				map.put("time", time);//秒s
				return new ReturnData(map,200,"success");
			} catch (Exception e) {
				e.printStackTrace();
				return new ReturnData(500,"Wow, that's wrong");
			}finally {
				if(jedis!=null){
					jedis.close();
				}
			}
		}

}
